package sdk.webview.fmc.com.fmcsdk.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import retrofit2.adapter.rxjava.HttpException;
import sdk.webview.fmc.com.fmcsdk.activity.H5Activity;

public class NetworkUtils {
	
	// 判断是否有网络
	public static boolean isAvailable(Context context) {
		ConnectivityManager mConnMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = mConnMan.getActiveNetworkInfo();
		if (info == null) {
			return false;
		}
		if(!H5Activity.isOnline){
			return false;
		}
		return info.isConnected();
	}

	// 判断网络是否为wifi连接
	public static boolean isWifi(Context context) {
		ConnectivityManager mConnMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = mConnMan.getActiveNetworkInfo();
		if (info == null) {
			return false;
		}
		return info.getType()==ConnectivityManager.TYPE_WIFI;
	}



	public static void exceptionToast(Context context,Throwable e){
		if (e instanceof HttpException){
			Toast.makeText(context,"服务器连接异常,请检查IP地址",Toast.LENGTH_SHORT).show();
		} else if (e instanceof ConnectException) {
			Toast.makeText(context,"请检查网络",Toast.LENGTH_SHORT).show();
		}else if (e instanceof SocketTimeoutException) {
			Toast.makeText(context,"连接超时，请重试",Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(context,"数据异常",Toast.LENGTH_SHORT).show();
		}
	}
}
